package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.ContractProduct;
import cn.itcast.domain.cargo.ContractProductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import utils.ImageUploadUtils;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cargo/contractProduct")
public class ContractProductController extends BaseController {

 @Reference
 private ContractProductService contractProductService;

 @Reference
 private FactoryService factoryService;


 /**
  * 分页查询所有的货物
  * 参数：
  * 当前页：page
  * 每页条数：size
  * 购销合同id：contractId
  */
 @RequestMapping("/list")
 public String list(@RequestParam(defaultValue = "1") int page,
                    @RequestParam(defaultValue = "5") int size, String contractId) {
  //1、根据购销合同的id，查询所有货物
  ContractProductExample example = new ContractProductExample();
  ContractProductExample.Criteria criteria = example.createCriteria();
  criteria.andContractIdEqualTo(contractId);
  PageInfo info = contractProductService.findAll(example, page, size);
  request.setAttribute("page", info);
  //2、为了展示所有的厂家数据下拉框，查询所有的生产货物的厂家
  //根据ctype=“货物”的查询生产厂家
  FactoryExample factoryExample = new FactoryExample();
  FactoryExample.Criteria factoryCriteria = factoryExample.createCriteria();
  factoryCriteria.andCtypeEqualTo("货物");
  List<Factory> list = factoryService.findAll(factoryExample);
  request.setAttribute("factoryList", list);
  //3.为了保存方便，需要将购销合同id，传入到request域中
  request.setAttribute("contractId", contractId);
  return "cargo/product/product-list";
 }

 /**
  * 新增或修改
  * /cargo/contractProduct/edit.do
  */
 @RequestMapping("/edit")
 public String edit(ContractProduct contractProduct, MultipartFile productPhoto) throws IOException {
  //设置企业属性
  contractProduct.setCompanyId(getLoginCompanyId());
  contractProduct.setCompanyName(getLoginCompanyName());
  //判断是否存在id
  if (StringUtils.isEmpty(contractProduct.getId())) {
   if (!productPhoto.isEmpty()) {
    String url = new ImageUploadUtils().upload(productPhoto.getBytes());
    contractProduct.setProductImage(url);
   }
   //不存在id，调用service新增
   contractProductService.save(contractProduct);
  } else {
   //存在id，调用service修改
   contractProductService.update(contractProduct);
  }
  //重定向
  return "redirect:/cargo/contractProduct/list.do?contractId=" + contractProduct.getContractId();
 }

 /**
  * 更新货物
  * /cargo/contractProduct/toUpdate.do?id=f5ef6385-c151-42ca-aa5a-63071f3cc497
  */
 @RequestMapping("/toUpdate")
 public String toUpdate(String id) {
  //根据id查询
  ContractProduct contractProduct = contractProductService.findById(id);
  //将货物合同存入request域
  request.setAttribute("contractProduct", contractProduct);
  //查询生产厂家
  FactoryExample factoryExample = new FactoryExample();
  FactoryExample.Criteria criteria = factoryExample.createCriteria();
  criteria.andCtypeEqualTo("货物");
  List<Factory> list = factoryService.findAll(factoryExample);
  request.setAttribute("factoryList", list);
  //跳转页面
  return "cargo/product/product-update";
 }

 /**
  * 货物删除
  * /cargo/contractProduct/delete.do?id=7ac27daa-d850-4a55-b227-701cd1076d3d&contractId=24f300f1-eaf1-40d1-aae9-5297b7ef842c
  */
 @RequestMapping("/delete")
 public String delete(String id, String contractId) {
  //业务逻辑
  contractProductService.delete(id);
  //重定向
  return "redirect:/cargo/contractProduct/list.do?contractId=" + contractId;
 }

 /**
  * 进入上传货物页面
  * /cargo/contractProduct/toImport.do?contractId=6affcd8f-098e-49d9-8bd8-62eb066ea239
  */
 @RequestMapping("/toImport")
 public String toImport(String contractId) {
  request.setAttribute("contractId", contractId);
  return "cargo/product/product-import";
 }

 /**
  * 批量上传货物
  * 通过EasyExcel完成文件上传解析，批量货物保存
  * /cargo/contractProduct/import.do
  */
 @RequestMapping("/import")
 public String importExcel(String contractId,MultipartFile file) throws Exception {
  List<ContractProduct> list = EasyExcel.read(file.getInputStream())
   .head(ContractProduct.class)
   .sheet(0)
   .doReadSync();

  for (ContractProduct contractProduct : list) {
   contractProduct.setContractId(contractId);
   contractProduct.setCompanyId(getLoginCompanyId());
   contractProduct.setCompanyName(getLoginCompanyName());
  }
  contractProductService.saveAll(list);
  return "redirect:/cargo/contractProduct/list.do?contractId=" + contractId;
 }

}